// pages/profile/profile.ts

// 用户信息接口
interface UserInfo {
  nickName: string;
  avatarUrl: string;
  userId: string;
  memberLevel: string;
  points: string;
  location: string;
}

// 订单数量接口
interface OrderCounts {
  unpaid: number;
  unshipped: number;
  shipped: number;
  comment: number;
  refund: number;
}

// 志愿者信息接口
interface VolunteerInfo {
  points: number;
  monthlyIncrease: number;
}

// 页面数据接口
interface IPageData {
  defaultAvatarUrl: string;
  userInfo: UserInfo;
  orderCounts: OrderCounts;
  volunteerInfo: VolunteerInfo;
  isLogin: boolean;
}

Page({
  data: {
    defaultAvatarUrl: '/assets/images/default_avatar.png',
    userInfo: {
      nickName: '张小花',
      avatarUrl: 'https://randomuser.me/api/portraits/women/44.jpg',
      userId: '10086',
      memberLevel: '黄金',
      points: '2,860',
      location: '贵州省贵阳市'
    },
    orderCounts: {
      unpaid: 2,
      unshipped: 1,
      shipped: 0,
      comment: 0,
      refund: 0
    },
    volunteerInfo: {
      points: 685,
      monthlyIncrease: 85
    },
    isLogin: true  // 默认已登录状态，实际应用中需要通过登录接口判断
  },

  onLoad(): void {
    // 获取用户信息，实际应用中应该从本地缓存或服务器获取
    this.checkLoginStatus();
  },

  checkLoginStatus(): void {
    // 实际应用中，这里应该检查用户的登录状态
    // wx.getStorageSync('token') 等方式获取登录态
    const token: string = wx.getStorageSync('token');
    if (!token) {
      this.setData({
        isLogin: false,
        userInfo: {} as UserInfo
      });
    }
  },

  // 登录
  login(): void {
    // 实际应用中，这里应该调用微信登录接口
    wx.showLoading({
      title: '登录中...',
    });
    
    setTimeout(() => {
      wx.hideLoading();
      this.setData({
        isLogin: true,
        userInfo: {
          nickName: '张小花',
          avatarUrl: 'https://randomuser.me/api/portraits/women/44.jpg',
          userId: '10086',
          memberLevel: '黄金',
          points: '2,860',
          location: '贵州省贵阳市'
        }
      });
      wx.showToast({
        title: '登录成功',
        icon: 'success'
      });
    }, 1000);
  },

  // 扫码
  onScanTap(): void {
    wx.scanCode({
      success: (res: WechatMiniprogram.ScanCodeSuccessCallbackResult) => {
        wx.showToast({
          title: '扫码成功',
          icon: 'success'
        });
        console.log('扫码结果：', res);
      },
      fail: (err: WechatMiniprogram.GeneralCallbackResult) => {
        console.log('扫码失败：', err);
      }
    });
  },

  // 跳转到订单页面
  goToOrders(e: WechatMiniprogram.TouchEvent): void {
    const type: string = e.currentTarget.dataset.type || 'all';
    wx.navigateTo({
      url: `/pages/orders/orders?type=${type}`
    });
  },

  // 跳转到商家入驻
  goToMerchant(): void {
    wx.navigateTo({
      url: '/pages/merchant/merchant'
    });
  },

  // 跳转到收藏页面
  goToFavorites(): void {
    wx.navigateTo({
      url: '/pages/favorites/favorites'
    });
  },

  // 跳转到地址管理
  goToAddress(): void {
    wx.navigateTo({
      url: '/pages/address/address'
    });
  },

  // 联系客服
  contactService(): void {
    wx.makePhoneCall({
      phoneNumber: '4008123123',
      success: () => {
        console.log('拨打客服电话成功');
      },
      fail: (err: WechatMiniprogram.GeneralCallbackResult) => {
        console.log('拨打客服电话失败：', err);
      }
    });
  },

  // 跳转到优惠券页面
  goToCoupons(): void {
    wx.navigateTo({
      url: '/pages/coupons/coupons'
    });
  },

  // 跳转到门票页面
  goToTickets(): void {
    wx.navigateTo({
      url: '/pages/tickets/tickets'
    });
  },

  // 跳转到酒店订单页面
  goToHotelOrders(): void {
    wx.navigateTo({
      url: '/pages/hotel_orders/hotel_orders'
    });
  },

  // 跳转到志愿服务页面
  goToVolunteer(): void {
    wx.navigateTo({
      url: '/pages/volunteer/volunteer'
    });
  },

  // 更多服务
  goToMore(): void {
    wx.navigateTo({
      url: '/pages/more_services/more_services'
    });
  },

  // 积分兑换
  exchangePoints(): void {
    wx.navigateTo({
      url: '/pages/points_exchange/points_exchange'
    });
  },

  // 查看活动详情
  viewActivityDetail(e: WechatMiniprogram.TouchEvent): void {
    const id: string = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/activity_detail/activity_detail?id=${id}`
    });
  },

  // 账户安全
  goToAccountSecurity(): void {
    wx.navigateTo({
      url: '/pages/account_security/account_security'
    });
  },

  // 帮助与反馈
  goToHelp(): void {
    wx.navigateTo({
      url: '/pages/help/help'
    });
  },

  // 关于我们
  goToAbout(): void {
    wx.navigateTo({
      url: '/pages/about/about'
    });
  },

  // 设置
  goToSettings(): void {
    wx.navigateTo({
      url: '/pages/settings/settings'
    });
  },

  onShareAppMessage(): WechatMiniprogram.Page.ICustomShareContent {
    return {
      title: '贵州四季康养文旅小程序',
      path: '/pages/home/home'
    };
  }
}); 